/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.gwc.yy.liuxue.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.gwc.yy.liuxue.entity.Visa;
import com.gwc.yy.liuxue.service.VisaService;

/**
 * 签证材料Controller
 * @author 黎俊涛
 * @version 2017-07-05
 */
@Controller
@RequestMapping(value = "${adminPath}/liuxue/visa")
public class VisaController extends BaseController {

	@Autowired
	private VisaService visaService;
	
	@ModelAttribute
	public Visa get(@RequestParam(required=false) String id) {
		Visa entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = visaService.get(id);
		}
		if (entity == null){
			entity = new Visa();
		}
		return entity;
	}
	
	@RequiresPermissions("liuxue:visa:view")
	@RequestMapping(value = {"list", ""})
	public String list(Visa visa, HttpServletRequest request, HttpServletResponse response, Model model) {
		visa.setUpdateBy(UserUtils.getUser());
		Page<Visa> page = visaService.findPage(new Page<Visa>(request, response), visa); 
		model.addAttribute("page", page);
		return "yy/liuxue/visaList";
	}

	@RequiresPermissions("liuxue:visa:view")
	@RequestMapping(value = "form")
	public String form(Visa visa, Model model) {
		model.addAttribute("visa", visa);
		return "yy/liuxue/visaForm";
	}

	@RequiresPermissions("liuxue:visa:edit")
	@RequestMapping(value = "save")
	public String save(Visa visa, Model model, RedirectAttributes redirectAttributes) {
		if(StringUtils.isEmpty(visa.getId()));
			visa.setStudent(UserUtils.getUser());
		if (!beanValidator(model, visa)){
			return form(visa, model);
		}
		
		visaService.save(visa);
		addMessage(redirectAttributes, "保存签证材料成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/visa/?repage";
	}
	
	@RequiresPermissions("liuxue:visa:edit")
	@RequestMapping(value = "delete")
	public String delete(Visa visa, RedirectAttributes redirectAttributes) {
		visaService.delete(visa);
		addMessage(redirectAttributes, "删除签证材料成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/visa/?repage";
	}

}